package com.youlu.campus.admin.community.service.impl;

import com.mongodb.client.result.UpdateResult;
import com.youlu.campus.admin.base.BaseService;
import com.youlu.campus.admin.community.dto.CommentQueryRequest;
import com.youlu.campus.admin.community.dto.CommentResponse;
import com.youlu.campus.admin.community.dto.CommentUpdateRequest;
import com.youlu.campus.admin.community.dto.InteractiveMqRequest;
import com.youlu.campus.admin.community.entity.ContentComment;
import com.youlu.campus.admin.community.service.CommentService;
import com.youlu.campus.base.base.InteractiveActionEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 评论管理接口实现类
 *
 * @author: YangJ
 * @date: 2020/12/11 14:08
 */
@Slf4j
@Service("commentService")
public class CommentServiceImpl extends BaseService implements CommentService {


    @Override
    public Page<CommentResponse> commentList(CommentQueryRequest request) {

        Query query = new Query();
        if (StringUtils.isNotBlank(request.getKey())) {
            Pattern pattern = Pattern.compile("^.*" + request.getKey() + ".*$", Pattern.CASE_INSENSITIVE);
            query.addCriteria(new Criteria().orOperator(Criteria.where("commentatorNickName").is(request.getKey()),
                    Criteria.where("commentContent").regex(pattern)));
        }
        if (StringUtils.isNotBlank(request.getType())) {
            query.addCriteria(Criteria.where("contentType").is(request.getType()));
        }
        long count = mongoTemplate.count(query, ContentComment.class);

        PageRequest pageRequest = PageRequest.of(request.getPage() - 1, request.getPageSize());
//        query.with(new Sort(Sort.Direction.DESC, "createdTime"));
        Sort sort=Sort.by(Sort.Direction.DESC, "createdTime");
        query.with(sort);
        List<ContentComment> commentList = mongoTemplate.find(query.with(pageRequest), ContentComment.class);
        List<CommentResponse> list = commentList.stream().map(comment -> {
            CommentResponse response = mapperFacade.map(comment, CommentResponse.class);
            response.setContentTypeStr(getContentTypeStr(comment.getContentType()));
            return response;
        }).collect(Collectors.toList());
        return PageableExecutionUtils.getPage(list, pageRequest, () -> count);
    }

    @Override
    public boolean updateComment(CommentUpdateRequest request) {

        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(request.getId()));
        Update update = new Update();
        update.set("show", request.getShow());
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, ContentComment.class);
        if (updateResult.getModifiedCount() > 0) {
            log.info("修改评论信息成功,ID:{}，show:{}", request.getId(), request.getShow());

            //维护内容互动信息
            ContentComment comment = mongoTemplate.findOne(query, ContentComment.class);
            if(comment.getLevel().equals(2)){
                Query query1 = new Query();
                query1.addCriteria(Criteria.where("id").is(comment.getCommentId()));
                comment = mongoTemplate.findOne(query1, ContentComment.class);
            }
            if(comment != null){
                int commentCount = 1;
                if (Boolean.FALSE.equals(request.getShow())) {
                    // 扣减评论数
                    commentCount = -1;
                }else if (Boolean.TRUE.equals(request.getShow())){
                    // 增加评论数
                    commentCount = 1;
                }
            }
        }

        return true;
    }

    private String getContentTypeStr(String s) {
        if (StringUtils.isBlank(s)) {
            return "";
        }
        switch (s) {
            case "DK":
                return "打卡";
            case "DT":
                return "动态";
            case "WZ":
                return "文章";
            case "SP":
                return "商品";
            case "PL":
                return "评论";
            default:
                return "其他";
        }

    }

}
